package com.example.androidlearning.sy3;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.androidlearning.R;

public class ImageViewActivity extends AppCompatActivity {

    ImageView imageView;
    Button nextImageBtn;
    Button opacityIncBtn;
    Button opacityDecBtn;
    Button preImageBtn;

    Drawable[] drawables;
    int nowImageId = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.imageview);

        init();
        setListeners();
    }


    private void init() {

        imageView = findViewById(R.id.imageview);
        nextImageBtn = findViewById(R.id.nextimage);
        opacityIncBtn = findViewById(R.id.opacityinc);
        opacityDecBtn = findViewById(R.id.opacitydec);
        preImageBtn = findViewById(R.id.preimage);

        drawables = new Drawable[4];
        drawables[0] = getResources().getDrawable(R.mipmap.richu_riluo_004);
        drawables[1] = getResources().getDrawable(R.mipmap.richu_riluo_015);
        drawables[2] = getResources().getDrawable(R.mipmap.zhenxian_niukou);
        drawables[3] = getResources().getDrawable(R.mipmap.zhenxian_niukou_001);

        imageView.setImageDrawable(drawables[nowImageId]);
    }

    private void setListeners() {

        nextImageBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nowImageId = nowImageId == drawables.length - 1 ?  0 : ++nowImageId;
                imageView.setAlpha(1f);
                imageView.setImageDrawable(drawables[nowImageId]);
            }
        });

        preImageBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nowImageId = nowImageId == 0 ? drawables.length -1 : --nowImageId;
                imageView.setAlpha(1f);
                imageView.setImageDrawable(drawables[nowImageId]);
            }
        });

        opacityIncBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                float alpha = imageView.getAlpha();
                if(alpha == 1f)
                    Toast.makeText(ImageViewActivity.this, "透明已为1，无法再增加", Toast.LENGTH_SHORT).show();
                imageView.setAlpha(alpha = alpha < 1 ? alpha + 0.1f : 1);
            }
        });

        opacityDecBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                float alpha = imageView.getAlpha();
                if(alpha == 0f)
                    Toast.makeText(ImageViewActivity.this, "透明已为0，无法再减小", Toast.LENGTH_SHORT).show();
                imageView.setAlpha(alpha = alpha > 0 ? alpha - 0.1f : 0);
            }
        });

    }


}
